﻿package com.designoutput.elements{
	import com.designoutput.enum.ElementType;
	import com.designoutput.utils.Unit;
	import com.designoutput.utils.UnitUtils;
	
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * 
	 * LineElement -> BaseElement ->Base
	 */	
	public class LineElement extends BaseElement
	{
		public function LineElement()
		{
			super();
		}
		
		override public function getClassName():String{
			return ElementType.LineElement; 
		}
				
		static public function getLineSize(nW:Number, nH:Number):Object
		{
			if(nW * nW + nH * nH < 100)
			{
				if(nW == 0) nH = 10;
				else
				{
					var alpha:Number = Math.atan(nH / nW);
					var nXSign:Number = nW > 0 ? 1 : -1;
					var nYSign:Number = nH > 0 ? 1 : -1;
					nW = nXSign * Math.abs(Math.floor(10 * Math.cos(alpha)));
					nH = nYSign * Math.abs(Math.floor(10 * Math.sin(alpha)));
				}
			}
			return {nW:nW, nH:nH};
		}
		
		override protected function updateElement():void
		{
			super.updateElement();
			
			graphics.clear();
			graphics.lineStyle(lineSize, lineColor, lineAlpha);
			graphics.moveTo(0, 0);
			graphics.lineTo(_width, _height);
			
			if(lineSize < 10)
			{
				graphics.lineStyle(10, 0, 0);
				graphics.moveTo(0, 0);
				graphics.lineTo(_width, _height);
			}
		}
		
		override public function getReferencePoints():Vector.<Point> {
			var points:Vector.<Point> = new Vector.<Point>();
			points.push(new Point(0, 0));
			points.push(new Point(width, height));
			
			var m:Matrix = getTransformMatrix();
			
			for(var i:int = 0; i < points.length; i++)
				points[i] = m.transformPoint(points[i]);
			
			return points;
		}
		
		override public function setReferencePoints(points:Vector.<Point>):void {
			var w:Number = points[1].x - points[0].x;
			var h:Number = points[1].y - points[0].y;
			
			x = points[0].x;
			y = points[0].y;
			width = Math.abs(w);
			height = Math.abs(h);
			skewX = h < 0 ? -180 : 0;
			skewY = w < 0 ? -180 : 0;
			scaleX = 1;
			scaleY = 1;
			rotation = 0;
			
			updateElement();
		}
		
		override public function toXML():XML
		{
			var xmlNode:XML = super.toXML();
			return xmlNode;
		}
		
		override  public function fromXML(xmlNode:XML):void
		{
			super.fromXML(xmlNode);
		}

	}
}